package io.wongd.lib_douyin.db

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.util.Date


class Converters {
    @TypeConverter
    fun fromTimestamp(value: Long?): Date? {
        return value?.let { Date(it) }
    }

    @TypeConverter
    fun dateToTimestamp(date: Date?): Long? {
        return date?.time
    }

    @TypeConverter
    fun fromStringList(value: List<String>?): String? {
        return value?.let { Gson().toJson(it) }
    }

    @TypeConverter
    fun fromJoinString(value: String?): List<String>? {
        return value?.let { Gson().fromJson(it, object : TypeToken<List<String>>() {}.type) }
    }

    @TypeConverter
    fun fromStringPairMap(value: Map<String, String>?): String? {
        return value?.let { Gson().toJson(it) }
    }

    @TypeConverter
    fun fromMapString(value: String?): Map<String, String>? {
        return value?.let { Gson().fromJson(it, object : TypeToken<Map<String, String>>() {}.type) }
    }


}